package com.zuozuo.syn;

import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;

public class Demo3 {
    public static void main(String[] args) throws InterruptedException {
        test2();
    }

    private static void test1() throws InterruptedException {
        ArrayList list = new ArrayList();
        for (int i = 0; i < 10000; i++) {
            new Thread(() -> {
                list.add(Thread.currentThread().getName());
            }).start();
        }
        Thread.sleep(2000);
        list.forEach(System.out::println);
        System.out.println(list.size());
    }
    private static void test2() throws InterruptedException {
        CopyOnWriteArrayList list = new CopyOnWriteArrayList();
        for (int i = 0; i < 10000; i++) {
            new Thread(() -> {
                list.add(Thread.currentThread().getName());
            }).start();
        }
        Thread.sleep(2000);
        list.forEach(System.out::println);
        System.out.println(list.size());
    }
}
